package com.fishpond.demo.activity

import android.content.Context
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import com.fishpond.demo.R
import com.fishpond.demo.base.MyBaseActivity
import com.fishpond.demo.databinding.ActivitySeaLayoutBinding
import com.water.fish.FishStatus
import com.water.fish.PetFish
import com.water.fish.ShoalFish
import com.zxn.mvvm.ext.jumpInTo

/**
 *  Updated by zxn on 2022/4/28.
 *  Created by zxn on 2021/12/20.
 */
class SeaLayoutActivity : MyBaseActivity() {

    companion object {
        @JvmStatic
        fun jumpTo(context: Context) {
            jumpInTo<SeaLayoutActivity>(context)
        }
    }

    private val mBinding by lazy {
        ActivitySeaLayoutBinding.inflate(layoutInflater)
    }

    override fun onCreateRootView(): View = mBinding.root

    private val petFish by lazy {
        PetFish().apply {

            moveSpeed = 120

            moveLeftResId = R.mipmap.ic_fish_pet_left_normal

            turnRightResId = R.mipmap.ic_fish_pet_right_turn
            //turnRightResId = R.mipmap.ic_fish_pet_right_turn_thirsty

            spurtLeftResId = R.mipmap.ic_fish_pet_left_normal_water

            thirstyLeftResId = R.mipmap.ic_fish_pet_left_thirsty

            gladLeftResId = R.mipmap.ic_fish_pet_left_normal_glad

            fatLeftResId = R.mipmap.ic_fish_pet_left_normal_fat

            prettyLeftResId = R.mipmap.ic_fish_pet_left_normal_pretty

            //tips.
            /*tipsList = mutableListOf(
                "炎炎夏日，来杯H2O~",
                "莫以善小而不为,莫以恶小而为之",
                "志不强者智不达",
                "一定要充分利用生活中的闲暇时光",
                "静坐常思己过",
            )*/
        }
    }

    /**
     * 鱼群
     */
    private val mShoalFish by lazy {
        ShoalFish().apply {
            moveSpeed = 100
        }
    }

    private val skinResList = mutableListOf(
        R.mipmap.bg_orange_fish_shoal,
        R.mipmap.bg_orange_fish_shoal,
        R.mipmap.bg_blue_fish_shoal
    )
    private var mFoodCount = 100
    private var mFoodType = 0

    override fun onInitView() {
        supportActionBar?.title = "SeaLayout"
        mBinding.run {

            mBinding.fishLayout.eatFoodListener {
                Log.i("TAG", "onInitView: eatFoodListener")
                Toast.makeText(mContext, "吃到了", Toast.LENGTH_SHORT).show()
            }

            mBinding.fishLayout.feedListener { layout, list ->
                Log.i("TAG", "onInitView: ${list.size}")
                mFoodCount--
                mFoodType++
                layout.maxFoodCount = if (mFoodCount >= 5) 5 else mFoodCount
                for (v in list) {
                    if (mFoodType % 2 == 0) {
                        v.setImageResource(R.mipmap.ic_food_vip)
                    } else {
                        v.setImageResource(R.mipmap.ic_food_normal)
                    }
                }
            }

            mBinding.fishLayout.touchMoveEndListener {
                Toast.makeText(mContext, "控制我移动一段", Toast.LENGTH_SHORT).show()
            }

            mBinding.fishLayout.setOnPetClickListener {
                Toast.makeText(mContext, "点我了", Toast.LENGTH_SHORT).show()
            }

            mBinding.fishLayout.setFishData(mutableListOf(petFish, mShoalFish))

            mBinding.fishLayout.shoalChangeListener { gifImageView ->
                gifImageView.setImageResource(skinResList.shuffled().take(1)[0])
            }
        }
    }

    private var status: Int = 0

    override fun showToast(msg: String) {
        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_sea_layout, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.btnTravel -> {//旅行
                status++
                when (status % 2) {
                    0 -> {
                        //旅行回来.
                        petFish.updateFishStatus(FishStatus.NORMAL)
                        mBinding.fishLayout.notifyDataSetChanged(0)

                    }
                    1 -> {
                        petFish.updateFishStatus(FishStatus.TRAVEL)
                        mBinding.fishLayout.notifyDataSetChanged(0)
                    }
                }
            }
            R.id.btnShort -> {
                status++
                when (status % 3) {
                    0 -> petFish.updateFishStatus(FishStatus.EXCITING)
                    1 -> petFish.updateFishStatus(FishStatus.FLESH_UP)
                    2 -> petFish.updateFishStatus(FishStatus.BEAUTIFY)
                }
                mBinding.fishLayout.notifyDataSetChanged(0)
            }
            R.id.btnFishAdd -> {
                mShoalFish.skinResList.add(R.mipmap.bg_blue_fish_shoal)
                mShoalFish.skinResList.add(R.mipmap.bg_clown_fish_shoal)
            }
            R.id.btnOpen -> {
                DemoActivity.jumpTo(mContext)
            }
            R.id.btnChange -> {
                petFish.moveLeftResId = R.mipmap.ic_fish_pet_left_normal_fen
                mBinding.fishLayout.notifyDataSetChanged()
            }
            R.id.btnSpray -> {
                petFish.updateFishStatus(FishStatus.THIRSTY)
                mBinding.fishLayout.notifyDataSetChanged(0)
            }
            R.id.btnFeed -> {
                mBinding.fishLayout.petFeeding = !mBinding.fishLayout.petFeeding
            }
        }
        return super.onOptionsItemSelected(item)
    }

}


/*mBinding.fishLayout.petChangeListener { petFish, petView, petAction ->
                    when (petAction) {
                        Move -> petView.ivPetFish.setImageResource(
                            when (petFish.fishStatus) {
                                FishStatus.BEAUTIFY -> R.drawable.ic_pet_move_pretty
                                FishStatus.FLESH_UP -> R.drawable.ic_pet_move_fat
                                FishStatus.EXCITING -> R.drawable.ic_pet_move_glad
                                FishStatus.THIRSTY -> R.drawable.ic_pet_move_thirsty
                                FishStatus.SPRAY -> R.drawable.ic_pet_move_full
                                FishStatus.NORMAL -> R.drawable.ic_pet_move_normal
                                else -> R.drawable.ic_pet_move_thirsty
                            }
                        )
                        TURN -> petView.ivPetFish.setImageResource(
                            when (petFish.fishStatus) {
                                FishStatus.THIRSTY -> R.drawable.ic_pet_turn_thirsty
                                FishStatus.SPRAY, FishStatus.NORMAL -> R.drawable.ic_pet_turn_normal
                                else -> R.drawable.ic_pet_turn_normal
                            }
                        )
                        REST -> petView.tips()
                    }
                }*/

/*mBinding.fishLayout.petChangeListener { petFish, petView, petAction ->
    when (petAction) {
        Move -> petView.ivPetFish.setImageResource(
            when (petFish.fishStatus) {
                FishStatus.BEAUTIFY -> R.mipmap.ic_pet_move_pretty
                FishStatus.FLESH_UP -> R.mipmap.ic_pet_move_fat
                FishStatus.EXCITING -> R.mipmap.ic_pet_move_glad
                FishStatus.THIRSTY -> R.mipmap.ic_pet_move_thirsty
                FishStatus.SPRAY -> R.mipmap.ic_pet_move_full
                FishStatus.NORMAL -> R.mipmap.ic_pet_move_normal
                else -> R.mipmap.ic_pet_move_thirsty
            }
        )
        TURN -> petView.ivPetFish.setImageResource(
            when (petFish.fishStatus) {
                FishStatus.THIRSTY -> R.mipmap.ic_pet_turn_thirsty
                FishStatus.SPRAY, FishStatus.NORMAL -> R.mipmap.ic_pet_turn_normal
                else -> R.mipmap.ic_pet_turn_normal
            }
        )
        REST -> petView.tips()
    }
}*/